package com.colin.orderservice10001.consumer;

import com.colin.common.status.OrderStatus;
import com.colin.orderservice10001.mapper.OrderMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class OrderDelayCloseConsumer {

    @Autowired
    private OrderMapper orderMapper;


    /**
     * 延迟队列消费者
     * 接收 订单号 判断订单状态是否需要超时关单
     * @param orderNum 订单号 ORDER_时间戳_用户id
     */
    @RabbitListener(queues = "dlq")
    public void orderDelayClose(String orderNum) {
        log.info("获取订单状态 ...");
        Integer status = orderMapper.selectOrderStatus(orderNum);

        if (OrderStatus.CREATED.equals(status)) {
            log.error("订单支付超时 关闭订单");
            orderMapper.updateStatus(orderNum, OrderStatus.CLOSED);
        }
    }
}
